<?php
namespace app\common\component\admin;

use think\Session;
use app\common\model\AdminRoles;
use app\common\model\AdminResourcesController;
use app\common\model\AdminResourcesAction;

class AdminService {
    public function login($data) {
    	$adminModel = AdminModel::get(['admin_name' => $data['admin_name']]);

		if($adminModel) {
			// 用户状态请查看数据表备注
			switch($adminModel -> status) {
	    		case 1:
	    			return [
	    				'code' => -1,
	    				'message' => '已禁止'
	    			];
	    		default:
	    			if(checkPasswordHash($data['password'], $adminModel -> password)) {
	    				$this -> clearLoginSession();

	    				Session::set('admin_id', $adminModel -> admin_id);
	    				Session::set('admin_name', $adminModel -> admin_name);
	    				Session::set('admin_role', $this -> makeRoles($adminModel -> role_id));

	    				return [
	    					'code' => 0,
	    					'message' => '登陆成功!'
	    				];
	    			}
	    	}
		}
    	return [
			'code' => -1,
			'message' => '账号或密码错误!'
		];
    }

	public function makeRoles($role_id) {
		$adminRole = AdminRoles::get(['role_id', $role_id]) -> toArray();
		$adminRole['resources'] = json_decode($adminRole['resources'], true);

		$resourceControllerIdsStr = '';
		foreach($adminRole['resources'] as $resourceControllerId => $resourceActionIds) {
			$resourceControllerIdsStr .= $resourceControllerId . ',';
		}
		$resourceControllerIdsStr = substr($resourceControllerIdsStr, 0, -1);

		$role = [];
		$resultAdminResourcesController = AdminResourcesController::where('resource_controller_id in(' . $resourceControllerIdsStr . ')') -> select();
		foreach($resultAdminResourcesController as $adminResourcesController) {
			$resultAdminResourcesAction = AdminResourcesAction::where('resource_controller_id = ' . $adminResourcesController -> resource_controller_id . ' AND resource_action_id in(' . $adminRole['resources'][$adminResourcesController -> resource_controller_id] . ')') -> select();

			foreach($resultAdminResourcesAction as $adminResourcesAction) {
				$role[] = $adminResourcesController -> namespace . '::' . $adminResourcesAction -> name;
			}
		}

		return $role;
	}

    public function clearLoginSession() {
    	// 清空相关session
		Session::delete('admin_id');
		Session::delete('admin_name');
		Session::delete('admin_role');
    }

    public function isLogin() {
    	if(Session::has('admin_id') && Session::has('admin_id') > 0) {
    		return [
    			'code' => 0,
    			'message' => '已登陆'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '未登陆'
		];
    }

	public function checkPayPassword($pay_password) {
		$getAdmin = $this -> getAdmin();

		if(checkPasswordHash($pay_password, $getAdmin['data']['admin'] -> password)) {
			return [
				'code' => 0,
				'message' => '安全密码正确!',
				'data' => [
					'admin' => $getAdmin
				]
			];
		}

		return [
			'code' => -1,
			'message' => '安全密码错误!'
		];
	}
	
    public function getAdmin($admin_id = null) {
    	if($admin_id && $admin_id > 0) {
    		$getAdmin = AdminModel::get(['admin_id' => $admin_id]);
    	} else if(Session::has('admin_id')) {
    		$getAdmin = AdminModel::get(['admin_id' => Session::get('admin_id')]);
    	} else {
    		$getAdmin = false;
    	}
			
		if($getAdmin) {
			return [
				'code' => 0,
				'data' => [
					'admin' => $getAdmin
				]
			];
		}

		return [
			'code' => -1,
			'message' => '该用户不存在'
		];
    }

    public function getAdmins($page = 10) {
    	return AdminModel::alias('a')
			-> join('league_admin_roles a_r', 'a_r.role_id = a.role_id')
			-> field(
				'a.admin_id, a.role_id, a.admin_name, a.status, ' .
				'a_r.description'
			)
			-> order('a.admin_id DESC')
			-> paginate($page);
	}

    public function create($data) {
    	$data['password'] = getPasswordHash($data['password']);

    	$adminModel = new AdminModel($data);
    	$adminModel -> status = 0;

		// $adminModel -> allowField(true) -> save();
    	if($adminModel -> save()) {
    		return [
    			'code' => 0,
    			'message' => '注册成功',
    			'data' => [
    				'admin' => $adminModel
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '注册失败'
		];
    }

    public function update($data, $admin_id = null) {
    	$adminModel = new AdminModel();

    	if($admin_id && $admin_id > 0) {
    		$updateAdmin = $adminModel -> where('admin_id', $admin_id) -> update($data);
    	} else if(Session::has('admin_id')) {
    		$updateAdmin = $adminModel -> where('admin_id', Session::get('admin_id')) -> update($data);
    	} else {
    		$updateAdmin = false;
    	}

    	if($updateAdmin) {
    		return [
    			'code' => 0,
    			'message' => '修改成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '修改失败'
		];
    }
    
    public function delete($admin_id) {
    	if(AdminModel::where('admin_id', $admin_id) -> delete()) {
    		return [
    			'code' => 0,
    			'message' => '管理员删除成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '管理员删除失败'
		];
	}
}
?>